package com.ruoyi.project.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.domain.FinRecordVoucher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import javax.annotation.Resource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.FinPackage;
import com.ruoyi.project.service.IFinPackageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账套Controller
 * 
 * @author abel
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/project/FinPackage")
public class FinPackageController extends BaseController
{
    @Resource
    private IFinPackageService finPackageService;

    /**
     * 查询账套列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinPackage finPackage)
    {
        startPage();
        List<FinPackage> list = finPackageService.selectFinPackageList(finPackage);
        return getDataTable(list);
    }

    /**
     * 导出账套列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:export')")
    @Log(title = "账套", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinPackage finPackage)
    {
        List<FinPackage> list = finPackageService.selectFinPackageList(finPackage);
        ExcelUtil<FinPackage> util = new ExcelUtil<FinPackage>(FinPackage.class);
        util.exportExcel(response, list, "账套数据");
    }

    /**
     * 获取账套详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(finPackageService.selectFinPackageById(id));
    }

    @PreAuthorize("@ss.hasPermi('project:FinPackage:query')")
    @GetMapping(value = "/getCurrentMonth")
    public AjaxResult getCurrentMonth()
    {
        FinPackage finPackage = finPackageService.getFinPackageById(ServletUtils.getPackageId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("packageId",finPackage.getId());
        resData.put("year",finPackage.getCurrentYear());
        resData.put("month",finPackage.getCurrentMonth());
        return success(resData);
    }

    /**
     * 新增账套
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:add')")
    @Log(title = "账套", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinPackage finPackage) throws IOException {
        return toAjax(finPackageService.insertFinPackage(finPackage));
    }

    /**
     * 修改账套
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:edit')")
    @Log(title = "账套", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinPackage finPackage)
    {
        return toAjax(finPackageService.updateFinPackage(finPackage));
    }


    @PreAuthorize("@ss.hasPermi('project:FinPackage:edit')")
    @Log(title = "损益结转", businessType = BusinessType.UPDATE)
    @PutMapping("/SY/end")
    public AjaxResult syEnd()
    {
        Long packageId = ServletUtils.getPackageId();
        FinPackage finPackage = finPackageService.getFinPackageById(packageId);
        finPackageService.syEnd(finPackage);
        return AjaxResult.success();
    }

    /**
     * 删除账套
     */
    @PreAuthorize("@ss.hasPermi('project:FinPackage:remove')")
    @Log(title = "账套", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(finPackageService.deleteFinPackageByIds(ids));
    }
}
